package com.study.talk.module.netty.handler;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.module.entity.User;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.AddUserRequest;
import com.study.talk.module.netty.message.response.AddUserResponse;
import com.study.talk.module.netty.message.response.AddUserSelfResponse;
import com.study.talk.module.service.FriendService;
import com.study.talk.module.service.InviteUserRecordService;
import com.study.talk.module.service.UserInfoService;
import com.study.talk.module.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 添加好友消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class ApplyAddUserRequestHandler extends SimpleChannelInboundHandler<AddUserRequest> {

    @Resource
    private UserService userService;

    @Resource
    private FriendService friendService;

    @Resource
    private InviteUserRecordService inviteUserRecordService;

    @Resource
    private UserInfoService userInfoService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, AddUserRequest msg) throws Exception {
        log.info("好友添加事件,{}",msg);
        //服务器响应此次好友添加请求
        AddUserSelfResponse addUserSelfResponse = new AddUserSelfResponse();
        addUserSelfResponse.setInvitedUserId(msg.getAddUserId());
        addUserSelfResponse.setDateTime(msg.getDateTime());

        //获取通信信道
        Session session = ctx.channel().attr(Attributes.SESSION).get();

        User addUser = userService.getById(msg.getAddUserId());

        //要添加的好友不存在
        if (ObjectUtils.isNull(addUser)){
            addUserSelfResponse.setSuccess(false);
            addUserSelfResponse.setReason("要添加的好友不存在！");
            ctx.channel().writeAndFlush(addUserSelfResponse);
            return;
        }

        //如果已经是好友了就不用重复添加
        boolean isFriend = friendService.queryIsFriend(msg.getUserId(), msg.getAddUserId());
        if (isFriend){
            addUserSelfResponse.setSuccess(false);
            addUserSelfResponse.setReason("对方已经是您的好友！请勿重复添加");
            ctx.channel().writeAndFlush(addUserSelfResponse);
            return;
        }

        addUserSelfResponse.setSuccess(true);
        addUserSelfResponse.setDateTime(msg.getDateTime());


        //查询是否已经添加过邀请记录，没有就向数据库中添加一条申请添加好友记录
        boolean isInvite = inviteUserRecordService.queryHasInviteFriend(msg.getUserId(), msg.getAddUserId());
        if (!isInvite){
            inviteUserRecordService.addInviteFriend(msg.getUserId(),msg.getAddUserId());
        }

        //发送给被添加好友方的消息
        AddUserResponse addUserResponse = new AddUserResponse();
        UserInfo userInfo = userInfoService.getById(msg.getUserId());
        addUserResponse.setInviteId(userInfo.getId());
        addUserResponse.setInviteName(userInfo.getNickName());
        addUserResponse.setInviteAvatar(userInfo.getAvatar());

        //查看对方是否在线，在线则直接发送
        userService.sendMessage(msg.getAddUserId(),addUserResponse);
        //告诉添加方服务器已成功接收处理
        addUserSelfResponse.setSuccess(true);
        userService.sendMessage(session.getUserId(),addUserSelfResponse);
    }
}
